package com.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crm.common.constant.HttpStatus;
import com.crm.common.result.AjaxResult;
import com.crm.domain.Clue;
import com.crm.domain.Record;
import com.crm.dto.RecordDto;
import com.crm.service.ClueService;
import com.crm.service.RecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clues/record")
public class RecordController {



    private final RecordService recordService;

    private final ClueService clueService;


    @GetMapping("/list")
    public AjaxResult listRecord(Long clueId){
        List<Record> records = recordService.list(Wrappers.<Record>lambdaQuery().eq(Record::getClueId, clueId));
        AjaxResult ajax = new AjaxResult();
        ajax.put("rows", records);
        ajax.put("total", records.size());
        ajax.put("code", HttpStatus.SUCCESS);
        return ajax;
    }

    @PostMapping
    public AjaxResult addRecord(@RequestBody RecordDto dto){
        Record record = new Record();
        BeanUtils.copyProperties(dto, record);
        record.setType("0");
        record.setNextTime(dto.getNextTime());
        record.setLevel(dto.getLevel().toString());
        record.setSubject(dto.getSubject().toString());
        recordService.save(record);
        Clue clue = clueService.getById(dto.getClueId());
        clue.setNextTime(dto.getNextTime());
        clue.setLevel(dto.getLevel());
        clue.setStatus(Clue.CLUE_STATUS_FOLLOWING);
        clueService.updateById(clue);
        return AjaxResult.success();
    }

}
